import {BroadcastEvent} from "./events/BroadcastEvent";
import Logger from './utils/Logger.js';

var WxNotificationCenter = require('./utils/WxNotificationCenter.js')

export default class AppInitializer {
    init(app, auid, token){
        // console.log(`==> screen size:${Beans.json(ScreenDimensions.screen)}, 1dp=${ScreenDimensions.pxperdp}px`);
        const echatIMSDK = app.globalData.im;
        const globalStore = app.globalData.globalStore;



        let sdkConfig = {};
        sdkConfig.host = 'api.echat.work'; // TODO: set with env
        sdkConfig.httpPort = 58082; // TODO: set with env
        sdkConfig.socketPort = 59092; // TODO: set with env
        sdkConfig.key = 'TSDKTEST00001'; // TODO: set with env
        sdkConfig.secret = '';
        sdkConfig.apiTransport = 'HTTP';
        sdkConfig.loginAuid = auid;
        sdkConfig.loginToken = token;
        sdkConfig.listeners = globalStore.sdkListener;
        sdkConfig.fileServerConfig = {
            use: 'local',
            client: 'wx',
            baseUrl: 'http://api.echat.work:58082', // TODO: set with env
            version: 'v1',
        };

        // app.userInfo.auid = sdkConfig.loginAuid;
        globalStore.userInfo.auid = sdkConfig.loginAuid;
        echatIMSDK.init(sdkConfig, function (sdk) {
            if(sdk){
                Logger.info('echatIMSDK 成功连接, 可以使用 EChatIMApis 请求数据了.');
                // DeviceEventEmitter.emit(BroadcastEvent.EVENT_SDK_INIT, sdk)
                WxNotificationCenter.postNotificationName(BroadcastEvent.EVENT_SDK_INIT, sdk);
            }
            else {
                Logger.info('echatIMSDK 初始化失败.');
            }
        });
    }
}

export const appInitializer = new AppInitializer();
